﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jhong.FunMVC
{
    public sealed class HttpFileCollection : IEnumerable<KeyValuePair<string, HttpPostedFile>>
    {
        private IDictionary<string, HttpPostedFile> _dic = new Dictionary<string, HttpPostedFile>();

        internal void Add(string key, HttpPostedFile value)
        {
            this._dic.Add(key, value);
        }

        public bool ContainsKey(string key)
        {
            return this._dic.Keys.Contains(key);
        }

        public ICollection<string> Keys
        {
            get { return this._dic.Keys; }
        }

        public bool TryGetValue(string key, out HttpPostedFile value)
        {
            return this._dic.TryGetValue(key, out value);
        }

        public ICollection<HttpPostedFile> Values
        {
            get { return this._dic.Values; }
        }

        public HttpPostedFile this[string key]
        {
            get
            {
                return this._dic[key];
            }
        }

        private IList<HttpPostedFile> _dicToList;
        public HttpPostedFile this[int index]
        {
            get
            {
                if (null == this._dicToList) this._dicToList = this._dic.Values.ToList();
                return this._dicToList[index];
            }
        }

        internal void Add(KeyValuePair<string, HttpPostedFile> item)
        {
            this._dic.Add(item);
        }

        public bool Contains(KeyValuePair<string, HttpPostedFile> item)
        {
            return this._dic.Contains(item);
        }

        public void CopyTo(KeyValuePair<string, HttpPostedFile>[] array, int arrayIndex)
        {
            this._dic.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return this._dic.Count(); }
        }

        public bool IsReadOnly
        {
            get { return this._dic.IsReadOnly; }
        }

        public IEnumerator<KeyValuePair<string, HttpPostedFile>> GetEnumerator()
        {
            foreach (var f in this._dic) yield return f;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}
